Page({
  data: {
    orderId: null,
    currentDate: '',
    newDate: '',
    newTime: '',
    minDate: '',
    baseUrl: 'http://localhost:9205'
  },

  onLoad: function(options) {
    if (options.orderId && options.currentDate) {
      // 分离日期和时间部分
      const dateTimeParts = options.currentDate.split(' ');
      const datePart = dateTimeParts[0];
      const timePart = dateTimeParts.length > 1 ? dateTimeParts[1] : '';

      this.setData({
        orderId: options.orderId,
        currentDate: options.currentDate,
        minDate: options.minDate || this.getTodayDate()
      });
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  // 获取今天的日期
  getTodayDate: function() {
    const today = new Date();
    return today.getFullYear() + '-' + 
           (today.getMonth() + 1).toString().padStart(2, '0') + '-' + 
           today.getDate().toString().padStart(2, '0');
  },

  // 日期选择器变化事件
  bindDateChange: function(e) {
    this.setData({
      newDate: e.detail.value
    });
  },

  // 时间选择器变化事件
  bindTimeChange: function(e) {
    this.setData({
      newTime: e.detail.value
    });
  },

  // 确认修改
  confirmModify: function() {
    if (!this.data.newDate || !this.data.newTime) {
      wx.showToast({
        title: '请选择新的日期和时间',
        icon: 'none'
      });
      return;
    }

    const newDateTime = this.data.newDate + ' ' + this.data.newTime;

    // 调用后端接口更新订单日期
    wx.request({
      url: `${this.data.baseUrl}/order/updateOrderDate`,
      method: 'GET',
      data: {
        id: this.data.orderId,
        date: newDateTime
      },
      success: (res) => {
        wx.showToast({
          title: '预约日期已更新',
          icon: 'success'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      },
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '更新失败，请重试',
          icon: 'none'
        });
      }
    });
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  }
}); 